package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        //查出一级分类
        List<CategoryEntity> entities = categoryService.getLevelOneCategorys();
        model.addAttribute("categorys", entities);//转发
        return "index";//返回视图地址
    }

    //index/catalog.json
    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJSON() {
        Map<String, List<Catelog2Vo>> map = categoryService.catalogJSON();
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        RLock lock = redisson.getLock("my-lock");
        lock.lock();//加锁
        try {
            System.out.println("加锁成功，执行业务..."+Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (InterruptedException e) {

        } finally {
            System.out.println("释放锁"+Thread.currentThread().getId());
            //解锁 假设解锁代码没有运行，那么会怎么样？还会出现死锁问题吗。
            lock.unlock();
        }
        return "hello";
    }

    /**
     * 车库停车
     * 信号量
     * 3车位
     */
    @GetMapping("/part")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore part = redisson.getSemaphore("part");
        part.acquire();//获取一个信号获取一个值
        //占车位
        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() throws InterruptedException {
        RSemaphore part = redisson.getSemaphore("part");
        part.release();//释放一个车位(信号)
        return "ok";
    }

    /**
     * 闭锁场景例子：放假锁门
     * 必须等待所有人走完才能锁门，假设等待5个班全部走完，就可以锁大门
     */
    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();//等待闭锁完成，具体多少人可以设置
        return "放假了..";
    }

    @GetMapping("/gogogo/{id}")
    @ResponseBody
    public String gogogo(@PathVariable("id") Long id){
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();//计数-1
        return id+"班的人走了..";
    }
}
